/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.gov.cemaden.library.dao.smp;

import br.gov.cemaden.library.dao.GenericDao;
import br.gov.cemaden.library.interfaces.smp.WhoSysInterface;
import br.gov.cemaden.library.model.smp.WhoSys;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author paulo.dias
 */
public class WhoSysDao extends GenericDao implements WhoSysInterface {

    private Session session;

    public WhoSysDao(Session session) {
        this.session = session;
    }

    public List<WhoSys> findAll() {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<WhoSys> whoSys = null;
        try {
            Query query = session.getNamedQuery("WhoSys.findAll").setMaxResults(30);
            whoSys = (List<WhoSys>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no WhoSysFacade.findAll: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return whoSys;
    }

    public WhoSys findById(Long id) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        WhoSys whoSys = null;
        try {
            Query query = session.getNamedQuery("WhoSys.findById").setParameter("id", id);
            whoSys = (WhoSys) query.uniqueResult();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no WhoSysFacade.findById: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return whoSys;
    }

    public WhoSys findByHostName(String hn) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        WhoSys whoSys = null;
        try {
            Query query = session.getNamedQuery("WhoSys.findByHostName").setParameter("hostName", hn);
            whoSys = (WhoSys) query.uniqueResult();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no WhoSysFacade.findByHostName: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return whoSys;
    }

    public WhoSys findByHostAddress(String host) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        WhoSys whoSys = null;
        try {
            Query query = session.getNamedQuery("WhoSys.findByHostAddress").setParameter("hostAddress", host);
            whoSys = (WhoSys) query.uniqueResult();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no WhoSysFacade.findByHostAddress: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return whoSys;
    }

    public WhoSys findByHostAddressAndHostName(String hostAddress, String hostName) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        WhoSys whoSys = null;
        try {
            Query query = session.getNamedQuery("WhoSys.findByHostAddressAndHostName")
                    .setParameter("hostAddress", hostAddress)
                    .setParameter("hostName", hostName);
            whoSys = (WhoSys) query.uniqueResult();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no WhoSysFacade.findByHostAddressAndHostName: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return whoSys;
    }

    public void insert(WhoSys whoSys) {
        try {
            saveOrUpdate(this.session, whoSys);
        } catch (Exception ex) {
            Logger.getLogger(WhoSysDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void update(WhoSys whoSys) {
        try {
            saveOrUpdate(this.session, whoSys);
        } catch (Exception ex) {
            Logger.getLogger(WhoSysDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void delete(WhoSys whoSys) {
        try {
            remove(this.session, whoSys);
        } catch (Exception ex) {
            Logger.getLogger(WhoSysDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
